
-- 注册滑动处理
function UIDungeonMain:initSlideTouchPanel()
    if not DungeonGuideM.isGuideFinished() then
        -- 新手关卡无法进行滑动开格子捡物品
        local slimeTouchPanel = findChildByName(self.node, "slide_touch_panel");
        slimeTouchPanel:setVisible(false);
        return;
    end

    -- 计算左上角格子和右下角格子的全局坐标
    local leftTopGridPos = self:getAllGrids()[1]:convertToWorldSpace(cc.p(0, 0));
    local rightBottomGridPos = self:getAllGrids()[TOTAL_GRID_NUM]:convertToWorldSpace(cc.p(0, 0));

    -- 格子之间的横向半径和垂直半径
    local horitzontalRadius = (rightBottomGridPos.x - leftTopGridPos.x) / 8;
    local verticalRadius = (leftTopGridPos.y - rightBottomGridPos.y) / 10;

    -- 边界坐标
    local minX = leftTopGridPos.x - horitzontalRadius;
    local maxX = rightBottomGridPos.x + horitzontalRadius;
    local minY = rightBottomGridPos.y - verticalRadius;
    local maxY = leftTopGridPos.y + verticalRadius;

    local slimeTouchPanel = findChildByName(self.node, "slide_touch_panel");
    slimeTouchPanel:setVisible(true);
    slimeTouchPanel:setSwallowTouches(false);

    self.slideGrids = {};
    for i = 1, TOTAL_GRID_NUM do
        self.slideGrids[i] = 0;
    end

    -- 尝试杀怪
    local function canAttack(grid)
        if not grid.monster then
            return false;
        end

        -- 检测英雄是否还活着
        if ME.user:isDead() then
            return false;
        end

        -- 怪物已死亡
        if grid.monster:isDead() then
            return false;
        end

        -- 需要判断是否能攻击
        if not grid:canHit() then
            return false;
        end

        return true;
    end

    -- 增加定时器，尝试对当前格子进行操作
    local function startSlideTick()
        if self.slideTickStarted then
            return;
        end

        local function doGridOp()
            if not self.slideTouchPostion then
                return;
            end

            local funcSetting = GameFuncSettingM.getFuncSetting();
            if funcSetting[1] ~= 1 then
                -- 功能未开启
                self.slideTickStarted = false;
                return;
            end

            local index = self.slideTouchPostion;

            -- 尝试打开
            if DungeonM.canOpenGrid(index) == GRID_OPEN_OK then
                DungeonActionM.go("open_grid", index);
                EventMgr.fire(event.PLAYER_MOVE, index);
            else
                -- 尝试捡物品
                local grid = DungeonM.getGridByPos(index);
                local uiGrid = self:getAllGrids()[index];
                if AutoFightM.canPickup(grid) then
                    if self.slideGrids[index] == 0 then
                        if DungeonM.isAllMonsterDead() then
                            if  AutoFightM.doPickup(grid) then
                                self.slideGrids[index] = 0;
                                EventMgr.fire(event.PLAYER_MOVE, index);
                            end
                        else
                            self.slideGrids[index] = 2;
                        end
                    else
                        self.slideGrids[index] = self.slideGrids[index] - 1;
                        if self.slideGrids[index] == 1 then
                            if  AutoFightM.doPickup(grid) then
                                self.slideGrids[index] = 0;
                                EventMgr.fire(event.PLAYER_MOVE, index);
                            end
                        end
                    end
                end

                -- 尝试杀怪
                if canAttack(grid) then
                    if self.slideGrids[index] == 0 then
                        self.slideGrids[index] = 6;
                    else
                        self.slideGrids[index] = self.slideGrids[index] - 1;
                        if self.slideGrids[index] == 5 then
                            DungeonActionM.go("physic_attack", grid:getPos());
                            EventMgr.fire(event.PLAYER_MOVE, index);
                            self.slideGrids[index] = 0;
                        end
                    end
                end
            end

            performWithDelay(slimeTouchPanel, doGridOp, 0.1);
        end
        performWithDelay(slimeTouchPanel, doGridOp, 0.1);
        self.slideTickStarted = true;
    end

    local function stopSlideTick()
        self.slideTickStarted = false;
        slimeTouchPanel:stopAllActions();
    end

    local isSliding = true;
    local function onSlide(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            local beganPos = sender:getTouchBeganPosition();
            local a = math.floor((beganPos.x - minX) / (horitzontalRadius * 2)) + 1;
            local b = GRID_ROW_NUM - (math.floor((beganPos.y - minY) / (verticalRadius * 2)) + 1);
            local gridIndex = b * GRID_COL_NUM + a;
            local grid = DungeonM.getGridByPos(gridIndex);

            -- 初始点击位置如果是怪物，不开启滑动功能，否则会和怪物长按显示信息冲突
            if grid and grid.monster then
                isSliding = false;
            end

        elseif eventType == ccui.TouchEventType.moved then
            if not isSliding then
                return;
            end

            local movePos = sender:getTouchMovePosition();

            if movePos.x < minX or movePos.x > maxX
                or movePos.y < minY or movePos.y > maxY then
                -- 处于边界外，不处理
                return;
            end

            local a = math.floor((movePos.x - minX) / (horitzontalRadius * 2)) + 1;
            local b = GRID_ROW_NUM - (math.floor((movePos.y - minY) / (verticalRadius * 2)) + 1);

            -- 算出现在处于哪个格子了
            local gridIndex = b * GRID_COL_NUM + a;
            self.slideTouchPostion = gridIndex;

            startSlideTick();
        else
            isSliding = true;
            self.slideTouchPostion = nil;
            stopSlideTick();
        end
    end

    slimeTouchPanel:addTouchEventListener(onSlide);
end